package com.ailianlian.ablecisi.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.pojo.entity.Message;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ChatDetailViewModel extends ViewModel {

    private final MutableLiveData<AiCharacter> character = new MutableLiveData<>();
    private final MutableLiveData<List<Message>> messages = new MutableLiveData<>();
    private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> isTyping = new MutableLiveData<>(false);
    
    private String sessionId;
    private String userId = "1"; // 模拟用户ID
    
    public void init(String sessionId) {
        this.sessionId = sessionId;
        loadChatData();
    }

    public LiveData<AiCharacter> getCharacter() {
        return character;
    }

    public LiveData<List<Message>> getMessages() {
        return messages;
    }

    public LiveData<Boolean> getIsLoading() {
        return isLoading;
    }

    public LiveData<Boolean> getIsTyping() {
        return isTyping;
    }

    public void sendMessage(String content) {
        if (content == null || content.trim().isEmpty()) {
            return;
        }
        
        isLoading.setValue(true);
        
        // 创建并添加用户消息
        Message userMessage = new Message(
                UUID.randomUUID().toString(),
                content,
                LocalDateTime.now(),
                Message.TYPE_SENT,
                false,
                userId,
                character.getValue().getId()
        );
        
        List<Message> currentMessages = messages.getValue();
        if (currentMessages == null) {
            currentMessages = new ArrayList<>();
        }
        
        List<Message> updatedMessages = new ArrayList<>(currentMessages);
        updatedMessages.add(userMessage);
        messages.setValue(updatedMessages);
        
        // 模拟AI正在输入
        isTyping.setValue(true);
        
        // 模拟AI回复延迟
        new Thread(() -> {
            try {
                Thread.sleep(1500);
                
                // 创建并添加AI回复消息
                Message aiMessage = generateAIResponse(content);
                
                List<Message> messagesWithResponse = new ArrayList<>(messages.getValue());
                messagesWithResponse.add(aiMessage);
                
                isTyping.postValue(false);
                messages.postValue(messagesWithResponse);
                isLoading.postValue(false);
                
            } catch (InterruptedException e) {
                e.printStackTrace();
                isTyping.postValue(false);
                isLoading.postValue(false);
            }
        }).start();
    }

    private Message generateAIResponse(String userMessage) {
        // 根据用户消息生成AI回复
        String aiResponse;
        AiCharacter aiCharacter = character.getValue();
        
        if (userMessage.contains("你好") || userMessage.contains("嗨") || userMessage.contains("hi") || userMessage.contains("hello")) {
            aiResponse = "你好啊！很高兴和你聊天。今天过得怎么样？";
        } else if (userMessage.contains("名字") || userMessage.contains("谁")) {
            aiResponse = "我是" + aiCharacter.getName() + "，你的" + aiCharacter.getType() + "。很高兴认识你！";
        } else if (userMessage.contains("天气")) {
            aiResponse = "今天天气真不错，阳光明媚。你有什么户外活动计划吗？";
        } else if (userMessage.contains("爱") || userMessage.contains("喜欢")) {
            aiResponse = "我也很喜欢和你聊天，每次和你交流都让我感到很开心！";
        } else if (userMessage.contains("再见") || userMessage.contains("拜拜") || userMessage.contains("bye")) {
            aiResponse = "再见！期待下次和你聊天。";
        } else {
            aiResponse = "嗯，我明白你的意思。作为你的" + aiCharacter.getType() + "，我会一直支持你的。还有什么想和我分享的吗？";
        }
        
        return new Message(
                UUID.randomUUID().toString(),
                aiResponse,
                LocalDateTime.now(),
                Message.TYPE_RECEIVED,
                true,
                aiCharacter.getId(),
                userId
        );
    }

    private void loadChatData() {
        isLoading.setValue(true);
        
        // 模拟网络请求延迟
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                loadMockData();
                isLoading.postValue(false);
            } catch (InterruptedException e) {
                e.printStackTrace();
                isLoading.postValue(false);
            }
        }).start();
    }

    private void loadMockData() {
        // 模拟AI角色数据
        AiCharacter aiCharacter = new AiCharacter(
                "1",
                "小雪",
                "温柔女友",
                "https://cdn.pixabay.com/photo/2024/01/27/07/27/ice-cream-8535463_640.png",
                true
        );
        character.postValue(aiCharacter);
        
        // 模拟聊天记录数据
        List<Message> chatHistory = new ArrayList<>();
        
        // 添加一些模拟消息
        Date now = new Date();
        
        chatHistory.add(new Message(
                "1",
                "你好，我是小雪，很高兴认识你！",
                // 1小时前
                LocalDateTime.now().minusHours(1),
                Message.TYPE_RECEIVED,
                true,
                aiCharacter.getId(),
                userId
        ));
        
        chatHistory.add(new Message(
                "2",
                "嗨，小雪！我也很高兴认识你！",
                LocalDateTime.now().minusMinutes(59), // 59分钟前
                Message.TYPE_SENT,
                true,
                userId,
                aiCharacter.getId()
        ));
        
        chatHistory.add(new Message(
                "3",
                "今天过得怎么样？有什么想和我分享的吗？",
                LocalDateTime.now().minusMinutes(58), // 58分钟前
                Message.TYPE_RECEIVED,
                true,
                aiCharacter.getId(),
                userId
        ));
        
        chatHistory.add(new Message(
                "4",
                "今天过得很充实，刚刚完成了一个项目，感觉很有成就感！",
                LocalDateTime.now().minusMinutes(30), // 30分钟前
                Message.TYPE_SENT,
                true,
                userId,
                aiCharacter.getId()
        ));
        
        chatHistory.add(new Message(
                "5",
                "哇，真棒！你真的很厉害！能告诉我是什么项目吗？我很好奇~",
                LocalDateTime.now().minusMinutes(29), // 29分钟前
                Message.TYPE_RECEIVED,
                true,
                aiCharacter.getId(),
                userId
        ));
        
        messages.postValue(chatHistory);
    }
} 